package com.loooooo.pm2024.service;

import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.TemplateSpec;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;

@Service

public class TemplateService {
//    public static class StringTemplateEngine extends SpringTemplateEngine {
//        public StringTemplateEngine() {
//        }
//    }

    TemplateEngine stringTemplateEngine;

    TemplateEngine getStringTemplateEngine() {
        if (stringTemplateEngine == null) {
            stringTemplateEngine = stringTemplateEngine();
        }
        return stringTemplateEngine;
    }

    TemplateEngine stringTemplateEngine() {
        StringTemplateResolver templateResolver = new StringTemplateResolver();
        TemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver);
        return engine;
    }

    public String process(String template, Context context) {
        TemplateSpec templateSpec = new TemplateSpec(template, TemplateMode.XML);
        String out = getStringTemplateEngine().process(templateSpec, context);

        return out;
    }

    public String process(String template, Context context, TemplateMode mode) {
        TemplateSpec templateSpec = new TemplateSpec(template, mode);
        String out = getStringTemplateEngine().process(templateSpec, context);

        return out;
    }
}
